package controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;

import db.HibernateUtil;
import db.HooldusteenuseTellimus;
import db.HooldusteenuseTellimuseKliendiOtsuseSeisundiLiik;
import db.HooldusteenuseTellimuseSeisundiLiik;

/**
 * Servlet implementation class OrderController
 */
@WebServlet("/order")
public class OrderController extends BaseController {
	private static final long serialVersionUID = 1L;

    @Override
    protected void get(HttpServletRequest request,
	    HttpServletResponse response, String username)
	    throws ServletException, IOException {
	RequestDispatcher view = request.getRequestDispatcher("/order.jsp");
	String id = request.getParameter("id");
	if(StringUtils.isNotBlank(id) && StringUtils.isNumeric(id)){
	    HooldusteenuseTellimus tel = HibernateUtil.getTellimusById(new Integer(id));
	    List<HooldusteenuseTellimuseSeisundiLiik> staatused = HibernateUtil.getHoolduseenuseTellimuseLiikList();
	    request.setAttribute("order", tel);
	    request.setAttribute("staatused", staatused);
	}	
	view.forward(request, response);
	
    }

    @Override
    protected void post(HttpServletRequest request,
	    HttpServletResponse response, String username)
	    throws ServletException, IOException {
	String id = request.getParameter("id");
	if(StringUtils.isNotBlank(id) && StringUtils.isNumeric(id)){
	    Integer orderNr = new Integer(id);
	    Session s = HibernateUtil.getSessionFactory().openSession();
	    s.beginTransaction();
	    HooldusteenuseTellimus t = (HooldusteenuseTellimus) s.get(HooldusteenuseTellimus.class, orderNr);
	    if(StringUtils.isNotBlank(request.getParameter("tuba"))){
		 t.setTubadeArv(new Integer(request.getParameter("tuba")));
	    }
	    if(StringUtils.isNotBlank(request.getParameter("sise"))){
		 t.setSiseruumidePindala(new BigDecimal(request.getParameter("sise")));
	    }	    
	    if(StringUtils.isNotBlank(request.getParameter("aed"))){
		 t.setAiaPindala(new BigDecimal(request.getParameter("aed")));
	    }	    
	    if(StringUtils.isNotBlank(request.getParameter("osutamiseAlgus"))){
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		 try {
		    t.setFirmaOtsusOsutamiseAlgus(f.parse(request.getParameter("osutamiseAlgus")));
		} catch (ParseException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
	    }
	    t.setPakkumiseTeade(request.getParameter("teade"));
	    
	    t.setHooldusteenuseTellimuseSeisundiLiik((HooldusteenuseTellimuseSeisundiLiik)
		    s.load(HooldusteenuseTellimuseSeisundiLiik.class, new Integer(request.getParameter("staatus"))));
	    s.getTransaction().commit();
	    
	    response.sendRedirect("/websocket/order?id="+orderNr);
	}
    }
    

}
